package com.zbkj.admin.config;

import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.core.query.QueryColumnBehavior;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import com.mybatisflex.spring.boot.SqlSessionFactoryBeanCustomizer;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Configuration;

/**
 * 自定义配置
 */
@Slf4j
@Configuration
public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer, ConfigurationCustomizer, SqlSessionFactoryBeanCustomizer {

    @Override
    public void customize(FlexConfiguration flexConfiguration) {

    }

    @Override
    public void customize(FlexGlobalConfig flexGlobalConfig) {
        AuditManager.setAuditEnable(true);
        //设置 SQL 审计收集器
        AuditManager.setMessageCollector(new ConsoleMessageCollector());
//        DialectFactory.registerDialect(DbType.MYSQL, new AuthDialectImpl());
        // 使用内置规则自动忽略 null 和 空白字符串
        QueryColumnBehavior.setIgnoreFunction(QueryColumnBehavior.IGNORE_BLANK);
        // 如果传入的值是集合或数组，则使用 in 逻辑，否则使用 =（等于）
        QueryColumnBehavior.setSmartConvertInToEquals(true);
        //
//        flexGlobalConfig.registerInsertListener(obj -> {
//            if (obj instanceof BaseEntity entity) {
//                log.debug("mybatis flex start insert InsertListener ....");
//                isCover(entity, BaseEntity::setUpdatedBy, SecurityUtil.getUserName(), false);
//                isCover(entity, BaseEntity::setUpdatedAt, LocalDateTime.now(), false);
//                isCover(entity, BaseEntity::setCreatedBy, SecurityUtil.getUserName(), false);
//                isCover(entity, BaseEntity::setCreatedAt, LocalDateTime.now(), false);
//            }
//        }, BaseEntity.class);
//        flexGlobalConfig.registerUpdateListener(obj -> {
//            if (obj instanceof BaseEntity entity) {
//                log.debug("mybatis flex start update UpdateListener ....");
//                isCover(entity, BaseEntity::setUpdatedBy, SecurityUtil.getUserName(), true);
//                isCover(entity, BaseEntity::setUpdatedAt, LocalDateTime.now(), true);
//            }
//        }, BaseEntity.class);
    }

    @Override
    public void customize(SqlSessionFactoryBean sqlSessionFactoryBean) {

    }

    /**
     * 是否覆盖原有值,避免更新操作手动入参
     *
     * @param obj         数据对象
     * @param fieldVal    属性新值
     * @param fieldOldVal 属性原值
     * @param isCover     是否覆盖原有值,避免更新操作手动入参
     */
//    public <P1, T extends Serializable> void isCover(T obj, SerBiConsumer<T, P1> fieldOldVal, P1 fieldVal, boolean isCover) {
//        String fieldName = LambdaUtil.getMethodName(fieldOldVal);
//        fieldName = STR."g\{fieldName.substring(1)}";
//        Object invoke = MethodUtil.invoke(obj, fieldName);
//        String setValueStr = StrUtil.str(invoke, Charset.defaultCharset());
//        if (StrUtil.isNotBlank(setValueStr) && !isCover) {
//            return;
//        }
//
//        if (obj instanceof BaseEntity) {
//            T finalObj = obj;
//            obj = GenericBuilder.of(() -> finalObj).with(fieldOldVal, fieldVal).build();
//        }
//    }

    /**
     * 自定义逻辑删除
     *
     * @return
     */
//    @Bean
//    public LogicDeleteProcessor logicDeleteProcessor() {
//        return new MyLogicDeleteProcessor();
//    }

}
